package com.zretc.support.service.serviceimpl;

import com.zretc.support.dao.ExamDao;
import com.zretc.support.entity.Exam;
import com.zretc.support.entity.Exampaper;
import com.zretc.support.service.ExamService;
import com.zretc.support.tool.NtpGetTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
public class ExamServiceImpl implements ExamService {
    @Autowired
    ExamDao examDao;
    @Autowired
    ExamPaperServiceImpl epsi;


    /**
     * 获得所有内容
     * @return List<Exam>
     */
    @Override
    @Cacheable(cacheNames = "com.zretc.support.service.serviceimpl.findAllExam")
    public List<Exam> findAllExam() {
        return examDao.findAll();
    }

    /**
     * 根据考试名称精确查找
     * @param examName
     * @return List<Exam>
     */
    @Override
    public List<Exam> findExamByName(String examName) {
        return examDao.getAllByExamName(examName);
    }

    /**
     * 根据考试id 删除
     * @param examId
     * @return 1
     */
    @Override
    public int deleteExam(Long examId) {

        return examDao.deleteByExamId(examId);
    }

    /**
     * 修改考试信息
     * @param exam
     * @return
     */
    @Override
    public int updateExam(Exam exam) {
        examDao.save(exam);
        return 1;
    }

    /**
     * 添加考试
     * @param exam
     * @return
     */
    @Override
    public int addExam(Exam exam) {

        return examDao.add(exam.getExamName(),exam.getExamBegin(),exam.getExamTime(),exam.getExampaper().getPaperId());
    }

    /**
     * 分页查询
     * @param keywords
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<Exam> getExamByPage(String keywords, int page, int pageSize) {
        Pageable pageable = PageRequest.of(page,pageSize);
        Page<Exam> pages = examDao.page(keywords,pageable);
        return pages;
    }

    @Override
    public Exam getExamById(long examId) {
        return examDao.getAllByExamId(examId);
    }

    /**
     * 前台 学生 我的考试 考试信息 分页
     *
     * @param studentId
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Page<Exam> getExamsByUserId(String studentId,String keywords, int page, int limit) {
        Pageable pageable = PageRequest.of(page - 1, limit);
        Page<Exam> pages = examDao.getExamsByUserId(studentId,keywords,pageable);
        //设置试卷总分
        for (Exam exam : pages.getContent()) {
            int paperId = exam.getExampaper().getPaperId();
            int count = epsi.countExampaperBypaperId(paperId);
            Exampaper ep = exam.getExampaper();
            ep.setCount(count);
            //开始考试时间
            Date begin = exam.getExamBegin();
            //获取现在的时间
            String nowstr = NtpGetTime.getDateTime();
            SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date now = null;
            try {
                now = simple.parse(nowstr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            long time = now.getTime() - begin.getTime();
            System.out.println(time+"时间间隔");
            long time1 =exam.getExamTime();
            if (time < 0) {
                //考试未开始
                exam.setCompareTime(0);
            }else if (time<15*60*1000 && time>0){
                //考试开始了，允许迟到15min
                exam.setCompareTime(1);
            }else if (time>15*60*1000 && time<time1*60*1000){
                //考试迟到
                exam.setCompareTime(2);
            }else if (time>time1*60*1000){
                //考试结束
                exam.setCompareTime(3);
            }
        }
        return pages;
    }
}
